/**
 * 
 */
package com.redoor.iot.utils;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redoor.iot.model.User;
import com.redoor.iot.security.MyUser;

/**
 * @author Administrator
 *
 */
public class SessionUtil {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 获取登录用户信息
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static User getUser() throws JsonParseException, JsonMappingException, IOException {
		//从security上下文中获取用户信息
		MyUser myPrincipal = (MyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String userInfo = myPrincipal.getLoginUserStr();
		ObjectMapper objectMapper = new ObjectMapper();
		User user = objectMapper.readValue(userInfo, User.class);
		return user;
	}

	public static Long getUserId() throws JsonParseException, JsonMappingException, IOException {
		return getUser().getUserId();
	}

	public static String getUserName() throws JsonParseException, JsonMappingException, IOException {
		return getUser().getUserName();
	}
}
